<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
        <el-form :model="form" :rules="rules" :disabled="mode==='show'" ref="dialogForm" label-width="100px" label-position="right">
			<el-form-item label="所属部门" prop="deptId">
				<el-cascader v-model="form.deptId" :options="options" :props="optionsProps" :show-all-levels="false" clearable style="width: 100%;"></el-cascader>
			</el-form-item>
            <el-form-item label="岗位名称" prop="postName">
                <el-input v-model="form.postName" placeholder="请输入岗位名称" clearable></el-input>
            </el-form-item>
			<el-form-item label="岗位编码" prop="postCode">
				<el-input v-model="form.postCode" placeholder="请输入岗位编码" clearable></el-input>
			</el-form-item>
            <el-form-item label="排序号" prop="orderNum">
                <el-input-number v-model="form.orderNum" clearable></el-input-number>
            </el-form-item>
			<el-form-item label="是否启用" prop="status">
				<el-switch v-model="form.status" active-value="1" inactive-value="0"></el-switch>
			</el-form-item>
            <el-form-item label="备注" prop="remark">
                <el-input type="textarea" v-model="form.remark" placeholder="请输入备注" clearable></el-input>
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="visible=false">取 消</el-button>
            <el-button v-if="mode!=='show'" type="primary" :loading="isSaving" @click="submit()">保 存</el-button>
        </template>
    </el-dialog>
</template>

<script>
    export default {
        emits: ['success', 'closed'],
        data() {
            return {
                // 表单元素控制
                mode: "add",
                titleMap: {
                    add: '新增',
                    edit: '编辑',
                    show: '查看'
                },
                visible: false,
                isSaving: false,
				// 所需数据选项
				options: [],
				optionsProps: {
					value: "id",
					label: "deptName",
					children: "children",
					emitPath: false,
					checkStrictly: true
				},
                // 表单数据
                form: {
                    deptId: null,
                    postCode: null,
                    postName: null,
                    orderNum: 1,
                    remark: null,
                    status: '1',
                },
                // 表单验证规则
                rules: {
                    postCode: [
                      {required: true, message: '请输入岗位编码', trigger: 'blur'}
                    ],
                    postName: [
                      {required: true, message: '请输入岗位名称', trigger: 'blur'}
                    ],
                    orderNum: [
                      {required: true, message: '请输入排序号', trigger: 'blur'}
                    ],
                    status: [
                      {required: true, message: '请选择状态', trigger: 'blur'}
                    ]
                }
            }
        },
        mounted() {
			this.getOptions();
        },
        methods: {
            // 显示
            open(mode = 'add') {
              this.mode = mode;
              this.visible = true;
              return this;
            },
            // 表单注入数据
            setData(data) {
              this.form.id = data.id;
              this.form.deptId = data.deptId;
              this.form.postName = data.postName;
              this.form.postCode = data.postCode;
              this.form.orderNum = data.orderNum;
              this.form.status = data.status;
              this.form.remark = data.remark;
            },
			// 加载树数据
			async getOptions() {
				this.options = await this.$API.manage.dept.list.get();
			},
            // 表单提交
            submit() {
                this.$refs.dialogForm.validate((valid) => {
                    if (valid) {
                        this.isSaving = true;
                        if (this.form.id) {
                            this.$API.manage.post.edit.put(this.form).then(() => {
                                this.$emit('success');
                                this.visible = false;
                                this.$message.success("保存成功");
                            }).catch(() => {
                                this.isSaving = false;
                            });
                        } else {
                            this.$API.manage.post.add.post(this.form).then(() => {
                                this.$emit('success');
                                this.visible = false;
                                this.$message.success("保存成功");
                            }).catch(() => {
                                this.isSaving = false;
                            });
                        }
                    }
                })
            }
        }
    }
</script>

<style>
</style>
